/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package freya.dao;

import freya.model.Evento;
import freya.util.HibernateUtil;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Danilo Rodrigues
 */
public class EventoDAO implements InterfaceEventoDAO {
    
    private final Session session;
    
    public EventoDAO() {  
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();  
    }

    @Override
    public void Insert(Evento evento) {
        try
        {
        Transaction tx_evento = (Transaction) session.beginTransaction();
        session.save(evento);  
        tx_evento.commit();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List List() {
        List<Evento> list = null;
        Transaction tx_evento = null;
        try
        {
            tx_evento = (Transaction) session.beginTransaction();
            list = (List<Evento>)session.createQuery("from Evento").list();
            for(Evento evento : list)
            {
                Hibernate.initialize(evento.getUsuario());
                Hibernate.initialize(evento.getSala());
            }
            
        }
        catch(Exception e) {
        }finally{
                tx_evento.commit();
            }
        return list;
    }

}
